var getStrCharLen = (val) => {
	var str = new String(val);
	var bytesCount = 0;
	for (var i = 0, n = str.length; i < n; i++) {
		var c = str.charCodeAt(i);
		if ((c >= 0x0001 && c <= 0x007e) || (0xff60 <= c && c <= 0xff9f)) {
			bytesCount += 1;
		} else {
			bytesCount += 2;
		}
	}
	return bytesCount;
}

var repeat = (str, n) => {
	return new Array(n+1).join(str);
}

var escape2Html = (str) => {
	if (!str) return '';
	return str.replace(/<image/gi, '<image style="max-width: 100%;height: auto;" ')
	.replace(/<section/g, '<div')
	.replace(/<article/g, '<div')
	.replace(/\/section>/g, '/div>')
	.replace(/\/article>/g, '/div>')
}

var formatTime = (number, format = 'Y年M月D日 h:m:s') => {
  var formateArr  = ['Y','M','D','h','m','s'];  
  var returnArr   = [];  
  
  var date = new Date(number * 1000);  
  returnArr.push(date.getFullYear());  
  returnArr.push(formatNumber(date.getMonth() + 1));  
  returnArr.push(formatNumber(date.getDate()));  
  
  returnArr.push(formatNumber(date.getHours()));  
  returnArr.push(formatNumber(date.getMinutes()));  
  returnArr.push(formatNumber(date.getSeconds()));  
  
  for (var i in returnArr)  
  {  
    format = format.replace(formateArr[i], returnArr[i]);  
  }  
  return format;  
}

var formatNumber = (n) => {
  n = n.toString()  
  return n[1] ? n : '0' + n 
}
// 解析URL 参数
function changeChinese(val){
	var str = null
	if(val == 1){
		str = '一'
	}else if(val == 2){
		str = '二'
	}else if(val == 3){
		str = '三'
	}else if(val == 4){
		str = '四'
	}else if(val == 5){
		str = '五'
	}
	return str

}
/* 
 * 处理富文本里的图片宽度自适应
 * 1.去掉img标签里的style、width、height属性
 * 2.img标签添加style属性：max-width:100%;height:auto
 * 3.修改所有style里的width属性为max-width:100%
 * 4.去掉<br/>标签
 * @param html
 * @returns {void|string|*}
 */
function formatRichText(html) {
	let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
		match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
		match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
		match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
		return match;
	});
	newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
		match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
		return match;
	});
	newContent = newContent.replace(/<br[^>]*\/>/gi, '');
	newContent = newContent.replace(/\<img/gi,
		'<img style="max-width:100%;height:auto;display:inline-block;margin:10rpx auto;"');
	return newContent;
}
export default {
	getStrCharLen,
	repeat,
  escape2Html,
  formatTime,
  changeChinese,
  formatRichText
}